/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.UserBean;
import com.minierp.dao.UserDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.User;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class UserValidator implements Validator {
	private UserDao userDao;

	public boolean supports(Class clazz) {
		return UserBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		UserBean bean = (UserBean) obj;
		User form = bean.getUser();
		if (form.getUserName() != null && !form.getUserName().trim().equals("")) {
			if (form.getId() == 0L
					&& (bean.getCurrentPassword() == null || bean
							.getCurrentPassword().trim().equals(""))) {
				errors.rejectValue("currentPassword", "", "请输入『密码』");
			} else if (form.getStafferId() == 0L) {
				errors.rejectValue("user.stafferId", "", "请选择『职员』");
			} else {
				boolean hasError = false;
				SimpleDateFormat e;
				if (!hasError) {
					if (form.getUseValidity().equals("Y")) {
						try {
							e = new SimpleDateFormat("yyyy-MM-dd");
							form.setValidity(e.parse(bean.getValidityStr()));
						} catch (Exception arg10) {
							hasError = true;
							errors.rejectValue("validityStr", "",
									"『有效期』填写错误，请按以下格式输入：2008-01-01");
						}
					} else {
						form.setValidity((Date) null);
					}
				}

				if (!hasError && form.getTimeLimit().equals("Y")) {
					e = new SimpleDateFormat("yyyy-MM-dd HH:mm");

					try {
						Date e1 = e.parse("2010-01-01 " + form.getTlStartHour()
								+ ":" + form.getTlStartMinute());
						Date end = e.parse("2010-01-01 " + form.getTlEndHour()
								+ ":" + form.getTlEndMinute());
						if (e1.after(end)) {
							hasError = true;
							errors.rejectValue("user.timeLimit", "",
									"『使用时间范围』开始时间不能大于结束时间");
						}
					} catch (Exception arg9) {
						hasError = true;
						errors.rejectValue("user.timeLimit", "", "『使用时间范围』错误："
								+ arg9.getMessage());
					}
				}

				if (!hasError) {
					User e2 = this.userDao.get(form.getUserName());
					if (form.getId() == 0L) {
						if (e2 != null) {
							hasError = true;
							errors.rejectValue("user.userName", "", "『用户名』已经存在");
						}
					} else if (e2 != null && e2.getId() != form.getId()) {
						hasError = true;
						errors.rejectValue("user.userName", "", "『用户名』已经存在");
					}
				}

				if (!hasError
						&& (form.getId() == 0L || bean.getCurrentPassword() != null
								&& !bean.getCurrentPassword().trim().equals(""))) {
					try {
						form.setPassword(EncCommonHelper.encPwd(bean
								.getCurrentPassword()));
					} catch (Exception arg8) {
						hasError = true;
						errors.rejectValue("currentPassword", "", "『密码』有错误："
								+ arg8.getMessage());
					}
				}
			}
		} else {
			errors.rejectValue("user.userName", "", "请输入『用户名』");
		}

	}

	public UserDao getUserDao() {
		return this.userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}